<template>
  <div>
    <network-error v-bind:show="showError"></network-error>
    <navbar title="房租缴费" backToApp></navbar>
    <router-view
      transition="fade"
      transition-mode="out-in"
      keep-alive>
    </router-view>
  </div>
</template>
<script>
import navbar from './components/Navbar'
import networkError from './components/NetworkError'

import weui from 'weui.js'
import ajax from './assets/ajax.js'
import $ from './assets/util.js'

export default {
  components: {
    navbar,
    networkError
  },
  data() {
    return {
      loading: null,
      showError: false
    }
  },
  beforeRouteEnter(to, from, next) {
    next((vm) => {
      vm.setUserData()
      vm.showLoading()
      vm.isRentStageUser()
    })
  },
  methods: {
    setUserData() {
      const query = this.$route.query
      const mobile = query.mobile
      const userId = query.userId
      const loginStatus = query.loginStatus
      this.$store.commit('SET_USER_MOBILE', mobile)
      this.$store.commit('SET_USER_ID', userId)
      this.$store.commit('SET_USER_LOGIN_STATUS', loginStatus)
      this.$store.commit('SET_RENT_STAGE_URL', { mobile, loginStatus })
    },
    showLoading() {
      this.loading = weui.loading('加载中...', {
        className: 'init-loading'
      })
    },
    handleAjaxData(methodId, data) {
      return ajax.handleData({
        methodId,
        data
      })
    },
    isRentStageUser() {
      // 是否是租金分期用户
      const data = this.handleAjaxData('J013', { mobile: this.$store.state.user.mobile })

      this.$http.post(
        ajax.url,
        data
      ).then((response) => {
        const code = response.body.code
        if (code === '0') {
          const existed = response.body.res.data.isExist
          if (existed === '1') {
            // 是，直接跳转租金分期页面
            // 此时租金分期不需要再返回交租
            window.location.href = this.$store.state.rentStage.urlWithoutFromInnjia
          } else {
            // 否，判断是否是认证用户
            this.isAuthUser()
          }
        } else if (code === '1') {
          // 找不到用户数据
          this.loading.hide()
          this.$router.push('rentMode')
        }else {
          // 手机号码为空
          this.loading.hide()
          this.showError = true
        }
      }, (errResponse) => {
        this.loading.hide()
        this.showError = true
      })
    },
    isAuthUser() {
      // 是否是认证用户
      const data = this.handleAjaxData('J005', { mobile: this.$store.state.user.mobile })

      this.$http.post(
        ajax.url,
        data
      ).then((response) => {
        // 隐藏loading
        this.loading.hide()
        const code = response.body.code
        if (code === '0') {
          const authed = response.body.res.data.authenticationStatus
          const rentStatus = ['Authenticated', 'CheckPending', 'TakeEffect', 'Invalid', 'CheckReturn']
          // 已认证用户跳转交租状态页
          if (rentStatus.indexOf(authed) > -1) {
            this.$router.push('rentStatus')
          } else {
            // 非认证用户前往交租方式页面
            this.$router.push('rentMode')
          }
        } else if(code === '1'){
          // 用户没有找到租约信息，跳转至交租方式页面
          this.$router.push('rentMode')
        }
      }, (errResponse) => {
        this.loading.hide()
        this.showError = true
      })

    }
  }
}
</script>

<style lang="less" scoped>
  .error {
    position: absolute;
    top: 40%;
    left: 50%;
    transform: translate(-50%, 0)
  }
</style>
